using Microsoft.AspNetCore.Mvc;
using ClockLib;
using ClockServer.Models;
using System;

namespace ClockServer.Controllers
{
    [ApiController]
    [Route("clock")]
    public class ClockController:ControllerBase
    {
        private ClockLib.Logger.ILogger? logger;
        private ClockSettings settings;
        private Clock clock;

        public ClockController(Clock clock,ClockSettings settings,ClockLib.Logger.ILogger? logger=null)
        {
            this.logger = logger;
            this.settings = settings;
            this.clock = clock;
        }

        [HttpGet("settings")]
        public ActionResult GetSettings()
        {
            return Ok(new ClockSettingsDto(settings));
        }

        [HttpPost("settings")]
        public ActionResult UpdateSettings([FromBody]ClockSettingsDto settings)
        {
            var changed = settings.Update(this.settings);
            if (changed) this.settings.Save();
            return Ok(settings);
        }
        [HttpGet("next")]
        public ActionResult GetNextAlarmTime()
        {
            return Ok(clock.GetNextAlarmTime());
        }

        [HttpGet("test")]
        public ActionResult TestAlarm(int hour, int minute,bool force)
        {
            clock.TestAlarm(hour, minute,force);
            return Ok();
        }
        //[HttpGet("remain")]
        //public ActionResult GetRemainSeconds()
        //{
        //    return Ok(clock.GetRemainAlarmSeconds());
        //}
    }
}